package com.csd.serviceImpl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

import com.csd.dao.DocumentRecordDAO;
import com.csd.dao.ProjectDAO;
import com.csd.exception.GetException;
import com.csd.exception.SaveException;
import com.csd.helper.ProjectState;
import com.csd.helper.SessionManager;
import com.csd.helper.Stage;
import com.csd.helper.Tool;
import com.csd.pojo.CostRecord;
import com.csd.pojo.DocumentRecord;
import com.csd.pojo.Project;
import com.csd.service.IDocumentRecordService;
import com.opensymphony.xwork2.ActionContext;
/**
 * 
 * Service：文档
 * @author csd
 * @version 1.0
 * 
 * <br> 修订时间：2009-12-17
 * <br> 修改原因：创建文件
 * <br> 修改人：何文深
 * 
 * <br> 
 * 
 */
public class DocumentRecordService implements IDocumentRecordService {
	private DocumentRecordDAO documentRecordDAO;
	
	private ProjectDAO projectDAO;
	
	
	public ProjectDAO getProjectDAO() {
		return projectDAO;
	}

	public void setProjectDAO(ProjectDAO projectDAO) {
		this.projectDAO = projectDAO;
	}

	public DocumentRecordDAO getDocumentRecordDAO() {
		return documentRecordDAO;
	}

	public void setDocumentRecordDAO(DocumentRecordDAO documentRecordDAO) {
		this.documentRecordDAO = documentRecordDAO;
	}

	public DocumentRecord getDocumentByID(Integer ID) {
		DocumentRecord record = null;

		try {
			record = documentRecordDAO.findById(ID);
		} catch (RuntimeException re) {
			re.printStackTrace();
		}

		return record;
	}

	/**
	 * 查询工程文档
	 * @param  projectID
	 * @return List<DocumentRecord>
	 */
	public List<DocumentRecord> listDocumentByProject(Integer projectID) {
		List<DocumentRecord> list = null;

		try {
			list = documentRecordDAO.findByProperty("project.id", projectID);
		} catch (RuntimeException re) {
			throw new GetException(re, "列出工程所有的文档");
		}


		return list;
	}

	/**
	 * 查询团队工程文档
	 * 
	 * @return List<DocumentRecord>
	 */
	public List<DocumentRecord> listTeamDocument() {
		List<DocumentRecord> list = null;

		try {
			int id = SessionManager.getUserID();
			List temp = (List)projectDAO.findByTeam(id);
			if(temp != null && temp.size()>0)
			{
				Project project = (Project)temp.get(0);
				list = documentRecordDAO.findByProperty("project", project);	
			}
		} catch (RuntimeException re) {
			throw new GetException(re, "列出文档异常");
		}

		return list;
	}
	/**
	 * 替换工程文档（无用）
	 * @param document
	 * @return boolean
	 */
	public boolean replaceDocument(DocumentRecord document) {
		boolean tag = false;

		try {
			documentRecordDAO.update(document);
			tag = true;
		} catch (RuntimeException re) {
			re.printStackTrace();
		}

		return tag;
	}
	/**
	 * 上载工程文档/更新工程文档
	 * @param document suffix stage
	 * @return boolean
	 */
	public boolean uploadDocument(File document,String suffix, String stage) {
		boolean tag = false;

		try {
			if(document!=null)
			{
				
					int id = SessionManager.getUserID();
					List list = (List)projectDAO.findByTeam(id);
					if(list != null && list.size()>0)
					{
						if( (((Project)list.get(0)).getState() != ProjectState.projectEnd)
								|| (((Project)list.get(0)).getState() != ProjectState.projectRejected) 
								|| (((Project)list.get(0)).getState() != ProjectState.prophaseReject))
						{
							String path = Tool.getPath();
							Project project = (Project)list.get(0);
							String projectpath = project.getProjectnum();
							if(projectpath == null) return false; 
							String mypath = "userfile" + File.separator + "upload"+File.separator+projectpath+File.separator+stage+suffix ;
							String filepath = path+mypath;
							
							if(Tool.createFile(document, filepath))	
							{
								DocumentRecord record  = documentRecordDAO.findByProjectAndState(project, stage);
								//如果有，则更新，如果没有则保存
								if(record == null )
								{
									record = new DocumentRecord();
									record.setName(stage);
									record.setPath(mypath);
									record.setProject((Project)list.get(0));
									record.setSize(""+document.length());
									record.setStage(stage);
									record.setTime(Tool.getCurrentDate());
									documentRecordDAO.save(record);	
								}else{
									File old = new File( path + record.getPath() );
									if(old != null)
										old.delete();
									record.setName(stage);
									record.setPath(mypath);
									record.setProject((Project)list.get(0));
									record.setSize(""+document.length());
									record.setStage(stage);
									record.setTime(Tool.getCurrentDate());
									documentRecordDAO.update(record);
								}
								tag = true;
							}
						}
					}

			}

		} catch (RuntimeException re) {
				throw new SaveException(re, "文档上传异常");
			}

		return tag;
	}

}
